
package com.bei_en.teacher.third.pay.web.weixin;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import com.bei_en.teacher.third.pay.web.weixin.http.HttpClientConnectionManager;

public class GetWxOrderno {

    public static DefaultHttpClient httpclient;

    /**
     *description:获取公众号支付预支付id
     *@param url
     *@param xmlParam
     *@return
     * @author ex_yangxiaoyi
     * @throws Exception 
     * @see
     */
    public static String getPayNo(String url, String xmlParam) throws Exception {

	httpclient = new DefaultHttpClient();
	httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);

	HttpPost httpost = HttpClientConnectionManager.getPostMethod(url);
	String prepay_id = "";
	String nonce_str = "";
	try {
	    httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
	    HttpResponse response = httpclient.execute(httpost);
	    String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	    System.out.println("getPayNo生成二维码返回:\n"+jsonStr);
	    Map map = doXMLParse(jsonStr);
	    if (jsonStr.indexOf("FAIL")!=-1) {
		throw new Exception((String) map.get("err_code_des"));
	    }
	    prepay_id = (String) map.get("prepay_id");
	    nonce_str = (String) map.get("nonce_str");
	} catch (Exception e) {
	    e.printStackTrace();
	    throw e;
	} finally {
	    httpclient.close();
	}

	return "["+prepay_id+","+nonce_str+"]";

    }

    /**
     *description:获取扫码支付连接
     *@param url
     *@param xmlParam
     *@return
     * @author ex_yangxiaoyi
     * @throws Exception 
     * @see
     */
    public static String getCodeUrl(String url, String xmlParam) throws Exception {

	httpclient = new DefaultHttpClient();
	httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);

	HttpPost httpost = HttpClientConnectionManager.getPostMethod(url);
	String code_url = "";
	try {
	    httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
	    HttpResponse response = httpclient.execute(httpost);
	    String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	    System.out.println("getCodeUrl生成二维码返回:\n"+jsonStr);
	    Map map = doXMLParse(jsonStr);
	    if (jsonStr.indexOf("FAIL")!=-1) {
		throw new Exception((String) map.get("err_code_des"));
	    }
	    code_url = (String) map.get("code_url");
	} catch (Exception e) {
	    e.printStackTrace();
	    throw e;
	} finally {
	    httpclient.close();
	}
	return code_url;
    }

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws Exception {

	if (null==strxml||"".equals(strxml)) {
	    return null;
	}

	Map m = new HashMap();
	InputStream in = String2Inputstream(strxml);
	SAXBuilder builder = new SAXBuilder();
	Document doc = builder.build(in);
	Element root = doc.getRootElement();
	List list = root.getChildren();
	Iterator it = list.iterator();
	while (it.hasNext()) {
	    Element e = (Element) it.next();
	    String k = e.getName();
	    String v = "";
	    List children = e.getChildren();
	    if (children.isEmpty()) {
		v = e.getTextNormalize();
	    } else {
		v = getChildrenText(children);
	    }

	    m.put(k, v);
	}

	// 关闭流
	in.close();

	return m;
    }

    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {

	StringBuffer sb = new StringBuffer();
	if (!children.isEmpty()) {
	    Iterator it = children.iterator();
	    while (it.hasNext()) {
		Element e = (Element) it.next();
		String name = e.getName();
		String value = e.getTextNormalize();
		List list = e.getChildren();
		sb.append("<"+name+">");
		if (!list.isEmpty()) {
		    sb.append(getChildrenText(list));
		}
		sb.append(value);
		sb.append("</"+name+">");
	    }
	}

	return sb.toString();
    }

    public static InputStream String2Inputstream(String str) {

	return new ByteArrayInputStream(str.getBytes());
    }

}